package com.trust.config;

import com.trust.agri.exception.ServiceException;
import com.trust.agri.response.ResponseResult;
import com.trust.agri.response.ResponseStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ccc
 * @desc
 * @since 2023/11/13 10:41
 */
@ControllerAdvice(annotations = {RestController.class})
public class SYQExceptionHandler {

    @ExceptionHandler(HttpMessageNotReadableException.class)
    @ResponseBody
    public ResponseResult<String> handleHttpMessageNotReadableException(HttpMessageNotReadableException e){
        ResponseResult<String> result = new ResponseResult<>();
        result.setCode(ResponseStatus.SERVER_ERROR.getCode());
        result.setMessage("入参格式错误" + e.getMessage());
        return result;
    }

    //@ExceptionHandler(ServiceException.class)
    @ResponseBody
    public ResponseResult<String> handleServiceException(ServiceException e){
        ResponseResult<String> result = new ResponseResult<>();
        result.setCode(ResponseStatus.SERVER_ERROR.getCode());
        result.setMessage(e.getMessage());
        return result;
    }

    //@ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseResult<String> handleException(Exception e){
        return ResponseResult.servicesFailed("未知异常，请联系管理员");
    }

}
